package com.ruoyi.cms.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.BaseSelect;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.cms.domain.CmsArticleCategory;
import com.ruoyi.cms.service.ICmsArticleCategoryService;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 文章分类Controller
 * 
 * @author zouhuu
 * @date 2022-08-04
 */
@RestController
@RequestMapping("/cms/cmsArticleCategory")
public class CmsArticleCategoryController extends BaseController
{
    @Autowired
    private ICmsArticleCategoryService cmsArticleCategoryService;

    /**
     * 查询文章分类选项
     */
    @GetMapping("/options")
    public AjaxResult options(CmsArticleCategory cmsArticleCategory)
    {
        List<BaseSelect> list = cmsArticleCategoryService.selectCmsArticleCategoryOptions(cmsArticleCategory);
        return AjaxResult.success(list);
    }

    /**
     * 查询文章分类列表
     */
    @PreAuthorize("@ss.hasPermi('cms:cmsArticleCategory:list')")
    @GetMapping("/list")
    public AjaxResult list(CmsArticleCategory cmsArticleCategory)
    {
        List<CmsArticleCategory> list = cmsArticleCategoryService.selectCmsArticleCategoryList(cmsArticleCategory);
        return AjaxResult.success(list);
    }

    /**
     * 导出文章分类列表
     */
    @PreAuthorize("@ss.hasPermi('cms:cmsArticleCategory:export')")
    @Log(title = "文章分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CmsArticleCategory cmsArticleCategory)
    {
        List<CmsArticleCategory> list = cmsArticleCategoryService.selectCmsArticleCategoryList(cmsArticleCategory);
        ExcelUtil<CmsArticleCategory> util = new ExcelUtil<CmsArticleCategory>(CmsArticleCategory.class);
        util.exportExcel(response, list, "文章分类数据");
    }

    /**
     * 获取文章分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('cms:cmsArticleCategory:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(cmsArticleCategoryService.selectCmsArticleCategoryById(id));
    }

    /**
     * 新增文章分类
     */
    @PreAuthorize("@ss.hasPermi('cms:cmsArticleCategory:add')")
    @Log(title = "文章分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CmsArticleCategory cmsArticleCategory)
    {
        cmsArticleCategory.setUserId(SecurityUtils.getUserId());
        cmsArticleCategory.setDeptId(SecurityUtils.getDeptId());
        cmsArticleCategory.setCreateBy(SecurityUtils.getUsername());
        return toAjax(cmsArticleCategoryService.insertCmsArticleCategory(cmsArticleCategory));
    }

    /**
     * 修改文章分类
     */
    @PreAuthorize("@ss.hasPermi('cms:cmsArticleCategory:edit')")
    @Log(title = "文章分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CmsArticleCategory cmsArticleCategory)
    {
        cmsArticleCategory.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(cmsArticleCategoryService.updateCmsArticleCategory(cmsArticleCategory));
    }

    /**
     * 删除文章分类
     */
    @PreAuthorize("@ss.hasPermi('cms:cmsArticleCategory:remove')")
    @Log(title = "文章分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        if (cmsArticleCategoryService.hasChildByCategoryId(id)) {
            return AjaxResult.error("存在子分类,不允许删除");
        }
        return toAjax(cmsArticleCategoryService.deleteCmsArticleCategoryById(id));
    }
}
